import axios from 'axios';
import { Message } from '@arco-design/web-vue';

const axiosInstance = axios.create({
  baseURL: import.meta.env.VITE_APP_BASE_API,
  timeout: 30000
});

axiosInstance.interceptors.request.use(
  (config) => {
    return config;
  },
  (error) => {
    return Promise.reject(error);
  }
);

axiosInstance.interceptors.response.use(
  (response) => {
    const { data } = response;
    return data;
  },
  (error) => {
    switch (error.response.status) {
      case 400:
        error.msg = '请求错误(400)';
        break;
      case 401:
        error.msg = '未授权，请重新登录(401)';
        break;
      case 403:
        error.msg = '拒绝访问(403)';
        break;
      case 404:
        error.msg = '请求出错(404)';
        break;
      case 408:
        error.msg = '请求超时(408)';
        break;
      case 500:
        error.msg = '服务器错误(500)';
        break;
      case 501:
        error.msg = '服务未实现(501)';
        break;
      case 502:
        error.msg = '网络错误(502)';
        break;
      case 503:
        error.msg = '服务不可用(503)';
        break;
      case 504:
        error.msg = '网络超时(504)';
        break;
      case 505:
        error.msg = 'HTTP版本不受支持(505)';
        break;
      default:
        error.msg = `连接出错(${error.response.status})!`;
    }
    Message.error(error.msg);
    return Promise.reject(error);
  }
);

export const request = (config) => {
  // 数据自定义处理
  return axiosInstance(config);
};
